<template>
  <div>
      <el-form :model="sitedata" label-width="120px">
      <el-form-item label="站点标题">
        <el-input v-model="sitedata.title" />
      </el-form-item>
      <el-form-item label="站点副标题">
        <el-input v-model="sitedata.subtitle" />
      </el-form-item>
      <el-form-item label="站点域名">
        <el-input v-model="sitedata.domain" />
      </el-form-item>

      <el-form-item label="站点LOGO">
        <el-upload
          class="avatar-uploader"
          :action="`${path}/fileUploadAndDownload/upload`"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
          :headers="header"
          list-type="picture-card"
          :on-preview="handlePictureCardPreview"
          :on-remove="handleRemove"
        >
          <img v-if="imageUrl" :src="imageUrl" class="avatar" />
          <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
        </el-upload>
      </el-form-item>

      <el-form-item label="站点关键字">
        <el-input v-model="sitedata.keywords" />
      </el-form-item>

      <el-form-item label="站点描述">
        <el-input v-model="sitedata.description" type="textarea" />
      </el-form-item>
      <el-form-item label="站点备案">
        <el-input v-model="sitedata.icp" />
      </el-form-item>

      <el-form-item label="站点模板">
        <el-select v-model="sitedata.theme" placeholder="请选择">
          <el-option label="default" value="default" />
        </el-select>
      </el-form-item>

      <el-form-item label="统计代码">
        <el-input v-model="sitedata.statistical" type="textarea" />
      </el-form-item>
      <el-form-item label="尾部信息">
        <el-input v-model="sitedata.copyrights" type="textarea" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="onSubmit">提交</el-button>
        <el-button>取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup>
import { onMounted, ref } from "vue";
import { divingGetSite,divingUpdateSite } from "@/api/sites/diving"
import { ElMessage } from 'element-plus'
import { useUserStore } from '@/pinia/modules/user'

  const sitedata = ref([])
  const imageUrl = ref([])
  const path = ref(import.meta.env.VITE_BASE_API)
  const header = ref({
   'x-token': ''
  })
  const handleAvatarSuccess = (
  response,
  uploadFile
) => {
  imageUrl.value = response.data.file.url
  sitedata.value.logo = response.data.file.url
}

const getList = async() => {
  const { data } = await divingGetSite()
  imageUrl.value = data.logo
  if(data.logo.indexOf("http") == -1 ){
    imageUrl.value = "http://diving.nbhysj.com" + data.logo 
  }  
  sitedata.value = data
}

const saveSite = async(data) => {
  const result = await divingUpdateSite(data)
  if(result.code == 0){
    ElMessage({
        message: result.msg,
        type: 'success',
      })
  } else {
    ElMessage.error(result.msg)
  }
}

onMounted(() => {
  getList()
  const {token} = useUserStore()
  header.value['x-token'] = token
})
const onSubmit = () => {  
  console.log('fileList.value')
  saveSite(sitedata.value)
}  
const beforeAvatarUpload = (rawFile) => {
  if (rawFile.type !== 'image/jpeg' && rawFile.type !== 'image/png') {
    ElMessage.error('图片必须是PNG或JPG格式')
    return false
  } else if (rawFile.size / 1024 / 1024 > 2) {
    ElMessage.error('图片大小不能大于 2MB!')
    return false
  }
  return true
}

</script>

<style lang="scss">
  .avatar-uploader .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>